const DEFAULT_PAGE = 'main.html';
const LOGIN_PAGE = 'login.html';

let rewritePage = (req, res, next) => {
  let { method, headers } = req;
  if (method === 'POST') {
    next();
    return;
  }
  if (!headers.accept || headers.accept.indexOf('text/html') === -1) {
    next();
    return;
  }

  if (req.url === '/login') {
    req.rewriteUrl = LOGIN_PAGE;
  } else {
    req.rewriteUrl = DEFAULT_PAGE;
  }

  next();
};

module.exports = rewritePage;
